package com.example.myapplication.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.adapter.DailyMenusAdapter;
import com.example.myapplication.R;
import com.example.myapplication.adapter.MainVpAdapter;
import com.example.myapplication.food_message.Menu;
import com.example.myapplication.fragment.VpFragment;
import com.example.myapplication.kit.MessageEvent;
import com.google.android.material.tabs.TabLayout;

import org.greenrobot.eventbus.EventBus;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;


public class MenuTendingMainActivity2 extends AppCompatActivity implements View.OnClickListener {
    Context mCText;
    private ArrayList<Menu> everdayMenuList;
    private Button tobreak;
    private LinearLayout btn_add, btn_delete;
    private ViewPager viewPager1;
    private int MenuId = 0;
    private TextView flBack;
    DailyMenusAdapter dailyMenusAdapter = null;
    private GridView dayGridView;
    private TreeSet<Integer> gridViewSize;
    private ImageView imageViewone;
    //all menu
    private Button back;
    private TabLayout tab;
    private ViewPager vp;
    private String[] type;
    private MainVpAdapter vpAdapter;
    private List<Fragment> fragments = new ArrayList<>();

    FrameLayout ll1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2_menu_tending);
        type = new String[]{"全部", "川菜", "鲁菜","贵菜","粤菜","湘菜"};
        init();
        initall();
        initView();
    }


    private void init() {
        mCText = MenuTendingMainActivity2.this;
        tobreak = findViewById(R.id.go_black);
        tobreak.setOnClickListener(this);
        //选中图标
        imageViewone = findViewById(R.id.menu_show_state);
        everdayMenuList = new ArrayList<>();
        //获取集合数据
        everdayMenuList = (ArrayList<Menu>) getIntent().getSerializableExtra("list");
        //创建构造器
        dailyMenusAdapter = new DailyMenusAdapter(mCText, everdayMenuList);
        dayGridView = findViewById(R.id.dayMenus);
        //使用构造器
        dayGridView.setAdapter(dailyMenusAdapter);
        btn_add = findViewById(R.id.home_forgment_but_add);
        btn_delete = findViewById(R.id.home_forgment_down_three_but2);

        ll1 =  findViewById(R.id.ll_allfood);

        btn_add.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
//        flBack =findViewById(R.id.fl_tx_back);
//        flBack.setOnClickListener(this);
        gridViewSize = new TreeSet<>();
        dayGridViewclick();


        //现实所有菜单
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.go_black:
                EventBus.getDefault().post(new MessageEvent(everdayMenuList));
                finish();
                break;
            case R.id.home_forgment_but_add:
                if (ll1.getVisibility() == View.GONE){
                    ll1.setVisibility(View.VISIBLE);
                }else {
                    ll1.setVisibility(View.GONE);
                }
//                LinearLayout ll2 = findViewById(R.id.ll_day_menu);
//                ll2.setVisibility(View.GONE);
                break;
            case R.id.home_forgment_down_three_but2:
                deleteGridViewSize();
                break;
//            case R.id.fl_tx_back:
//                FrameLayout ll11 = findViewById(R.id.ll_allfood);
//                ll11.setVisibility(View.GONE);

        }
    }

    /**
     * 启用GV点击事件
     */
    private void dayGridViewclick() {
        dayGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Menu menu = everdayMenuList.get(position);
                menu.setmImgState(menu.getmImgState() == 0 ? 1 : 0);
                if (menu.getmImgState() == 1) {
                    gridViewSize.add(position);
                }
                dailyMenusAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * 对今日菜单进行数据删除
     */
    private void deleteGridViewSize() {
        if (gridViewSize.size() == 0) {
            Toast.makeText(MenuTendingMainActivity2.this, "未选中菜品，请重新选择", Toast.LENGTH_SHORT).show();

        } else {
            int ff = 0;
            for (int i : gridViewSize) {
                everdayMenuList.remove(i - ff);
                //每次集合删除完毕数据之后长度会进行减一操作，所以就直接把删除的数据下标加上已删除的数据下标即可
                ff += 1;
            }
            //清除数组集合
            gridViewSize.clear();
            //刷新数据
            dailyMenusAdapter.notifyDataSetChanged();
            Toast.makeText(MenuTendingMainActivity2.this, "菜品删除成功", Toast.LENGTH_SHORT).show();//事件已经被消费，不要在处理了
            //dayGridView.setOnItemClickListener(null);
        }
    }

    //现实所有菜单
    private void initall() {

        tab = (TabLayout) findViewById(R.id.tab);
        vp = (ViewPager) findViewById(R.id.vp);
        //设置可滑动tab
        tab.setTabMode(TabLayout.MODE_SCROLLABLE);
        //tab点击事件
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override //tab点击
            public void onTabSelected(TabLayout.Tab tab) {
                //当点击 vp就滑动到对应一页
                vp.setCurrentItem(tab.getPosition());
            }

            @Override //tab未被选中
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override //tab重新选中
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        //vp设置监听
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override //页面滑动结束调用
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override //页面被选中调用 紧随页面滑动结束
            public void onPageSelected(int position) {
                tab.selectTab(tab.getTabAt(position));
            }

            @Override //页面滑动状态改变
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initView() {
        //设置tab数据 遍历
        for (int i = 0; i < getTabListCount(); i++) {
            //新建一个tab
            TabLayout.Tab t = tab.newTab();
            //设置新建的tab文字属性
            t.setText(type[i]);
            //增加到控件当中
            tab.addTab(t);
            //新建对应的fragment 构造器需要一个集合数据 数据随机给出
            fragments.add(new VpFragment(getRandomData()));
        }
        //新建vp适配器 第一个参数固定，第二个参数随便填，第三个参数是需要的数据
        vpAdapter = new MainVpAdapter(getSupportFragmentManager(), 0, fragments);
        //vp设置适配器
        vp.setAdapter(vpAdapter);
    }

    //可以由网络加载完数据后再调用此方法 返回菜品类型长度
    private int getTabListCount() {
        return type.length;
    }

    //给出随机长度数据
    private List<Menu> getRandomData() {
        //返回的集合
        List<Menu> foods = new ArrayList<>();
        //随机数
        int randomCount = (int) (Math.random() * 25) + 10;
        //添加等量数据
        for (int i = 0; i < randomCount; i++) {
            Menu exampleFood = new Menu();
            exampleFood.setmName("测试食物" + i);
            exampleFood.setmMoney(i+ randomCount);
            exampleFood.setmMoneys(i);
            foods.add(exampleFood);
        }
        return foods;
    }

}

